Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic

Imports Framework


'Table-Row Class (Customisable half)
Partial Public Class CContent

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache)
    Protected Friend Sub New(contentSetId As Integer, contentTemplateId As Integer)
        MyBase.New(contentSetId, contentTemplateId)
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, contentSetId As Integer, contentTemplateId As Integer)
        MyBase.New(dataSrc, contentSetId, contentTemplateId)
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal contentSetId As Integer, contentTemplateId As Integer, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, contentSetId, contentTemplateId, txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
		m_contentCreated = DateTime.Now
	End Sub
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"
	'Relationships - Foriegn Keys (e.g parent)
	Public ReadOnly Property [TemplateSet]() As CTemplateSet
		Get
			Return CTemplateSet.Cache.GetById(Me.ContentSetId)
		End Get
	End Property
	Public ReadOnly Property [Template]() As CTemplate
		Get
			Return CTemplate.Cache.GetById(Me.ContentTemplateId)
		End Get
	End Property

	'Relationships - Collections (e.g. children)

#End Region

#Region "Properties - Customisation"
	'Derived/ReadOnly (e.g. xml classes, presentation logic)

#End Region

#Region "Save/Delete Overrides"
	Public Overrides Sub Save(txOrNull As IDbTransaction)
		ContentModified = DateTime.Now
		ContentMD5 = CBinary.MD5_(ContentText)

		MyBase.Save(txOrNull)
	End Sub
#End Region

#Region "Custom Database Queries"
	'(Not normally required for cached classes, use list class for searching etc)
	'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
	'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region



#Region "Caching Details"
	'Cache Key
	Friend Shared CACHE_KEY As String = GetType(CContent).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CContentList
        Return New CContent().SelectAll()
    End Function
    
    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CContentList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub
    
    'Helper Method
    Private Function CacheGetById(ByVal list As CContentList) As CContent
        Return list.GetById(Me.ContentSetId, Me.ContentTemplateId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(target as CDataSrc, txOrNull as IDbTransaction) As CContent ', parentId As Integer) As CContent
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CContent(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.ContentSetId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
